import Logger from "./logger";

export type EnvType = "prod" | "test" | "dev" | "uat";
export type SdkEnv = {
    apiUrl: string,
    clientId: string,
    clientSecret: string,
    cnct: string,
    protocolType: string,
    encryptAESKey: string,
    decryptAESKey: string,
    encryptAESIv: string,
    decryptAESIv: string,
}

const SdkEnvConfig: Record<EnvType, SdkEnv> = {
    prod: {
        apiUrl: "https://www.derucci-smart.com",
        clientId: "hwzx",
        clientSecret: "WDFTpoase!CNZTzd&S5xQDCX",
        cnct: "hwzx-2QN4-a",
        protocolType: "wx",
        encryptAESKey: "SW26mbdiKFgZvb5p",
        encryptAESIv: "xO3t3bPN2rec3EYH",
        decryptAESKey: "afdn2rx1k8rworgh",
        decryptAESIv: "cbvosoy30zgcmmgh",
    },
    uat: {
        apiUrl: "https://test.derucci-smart.com",
        clientId: "hwzx",
        clientSecret: "ADFHpoase@CNZTzd#S5xOKYG",
        cnct: "hwzx-2QN4-a",
        protocolType: "wx",
        encryptAESKey: "SW26mbdiKFgZvb5p",
        encryptAESIv: "xO3t3bPN2rec3EYH",
        decryptAESKey: "afdn2rx1k8rworgh",
        decryptAESIv: "cbvosoy30zgcmmgh",
    },
    test: {
        apiUrl: "http://testdev.derucci-smart.com:880",
        clientId: "hwzx",
        clientSecret: "EFRBpoase&CNZTzd@S5xKUYF",
        cnct: "hwzx-2QN4-a",
        protocolType: "ws",
        encryptAESKey: "SW26mbdiKFgZvb5p",
        encryptAESIv: "xO3t3bPN2rec3EYH",
        decryptAESKey: "afdn2rx1k8rworgh",
        decryptAESIv: "cbvosoy30zgcmmgh",
    },
    dev: {
        apiUrl: "http://dev.derucci.smart",
        clientId: "hwzx",
        clientSecret: "Wrghpoase#CNZTzd@S5xyMiR",
        cnct: "hwzx-2QN4-a",
        protocolType: "ws",
        encryptAESKey: "SW26mbdiKFgZvb5p",
        encryptAESIv: "xO3t3bPN2rec3EYH",
        decryptAESKey: "afdn2rx1k8rworgh",
        decryptAESIv: "cbvosoy30zgcmmgh",
    },
}


/**
 * Env 类
 * 
 * 该类用于管理当前项目运行的环境（生产/测试）以及对应的 API 根地址。
 * 使用单例模式实现，保证全局唯一。
 */
class Env {
    private static instance: Env;
    private env: EnvType = "prod"; // 默认是生产环境

    private constructor() {}

    public static getInstance(): Env {
        if (!Env.instance) {
            Env.instance = new Env();
        }
        return Env.instance;
    }

    // 获取当前环境的 API 地址
    public get apiUrl(): string {
        return SdkEnvConfig[this.env].apiUrl;
    }

    public get clientId(): string {
        return SdkEnvConfig[this.env].clientId;
    }

    public get clientSecret(): string {
        return SdkEnvConfig[this.env].clientSecret;
    }

    public get cnct(): string {
        return SdkEnvConfig[this.env].cnct;
    }

    public get protocolType(): string {
        return SdkEnvConfig[this.env].protocolType;
    }

    public get encryptAESKey(): string {
        return SdkEnvConfig[this.env].encryptAESKey;
    }
    
    public get decryptAESKey(): string {
        return SdkEnvConfig[this.env].decryptAESKey;
    }


    public get encryptAESIv(): string {
        return SdkEnvConfig[this.env].encryptAESIv;
    }
    
    public get decryptAESIv(): string {
        return SdkEnvConfig[this.env].decryptAESIv;
    }

    // 设置当前环境
    public setEnv(env: EnvType): void {
        Logger.log("设置当前环境", env);
        this.env = env;
    }

    public get getEnv() {
        return this.env;
    }
}

export default Env.getInstance();